import random
# 0
# 就这么处理
try:
    a = [4, 3, 5]
    print(a[5])
except IndexError as e:
    print(e)

# 1
# 可以包含多个
try:
    1/0
    b = [4, 3, 3, 4, 5, 5, 7, 1]
    print(b[5])
except (IndexError,ZeroDivisionError) as e:
    print(e)


#2
try:
    b = [4, 3, 3, 4, 5, 5, 7, 1]
    print(b[5])
except IOError as e:
    print(e)

#4
#使用finally里close文件

# raise KeyboardInterrupt
# 就这样直接抛出错误

def guessNum():
    secret = random.randint(1,10)
    print('i love fishc')
    temp = input('猜猜看吧')
    try:
        guess = int(temp)
    except (ValueError,EOFError,KeyboardInterrupt):
        print('输入错误!')
        guess = secret
    while guess != secret:
        temp = input('猜错了,掉血,继续猜')
        guess = int(temp)
        if guess == secret:
            print('被猜到了')
        else:
            if guess > secret:
                print('大啦')
            else:
                print('小拉')
# guessNum()


def tryinput():
    while 1:
        try:
            int(input(''))
            break
        except:
            print('请输入整数')
    print('输入正确')
# tryinput()


def closeFile():
    try:
        f = open('gundam.txt')
        print(f.read())
    except OSError as reason:
        print('出错了:'+ str(reason))
    finally:
        if 'f' in locals():
            #如果文件对象变量存在当前局部变量符号表,说明打开成功
            f.close()
